provider "alicloud" {
  region                = local.region
}

terraform {
  required_version      = "~> 0.12"  
  backend "oss" {
    bucket              = "deeproute-terraform-resources"
    key                 = "stag/services/stg-ack-edge-cluster"
    tablestore_endpoint = "https://terraform-status.cn-shenzhen.ots.aliyuncs.com"
    tablestore_table    = "terraform_statelock_table"    
    region              = "cn-shenzhen"
  }
}

locals {
  vpc                   = "vpc-wz9nh1bbw1cbmdnknw5g7"
}

# // Add nat gateway access pod access internal
# resource "alicloud_nat_gateway" "main" { //按量付费
#   name          = local.fullname
#   nat_type      = "Enhanced"
#   vpc_id        = local.vpc
#   vswitch_id    = "vsw-wz9bujt34c241k8nqk2x4"
# }

# resource "alicloud_eip_address" "main" { //按量付费
#   address_name    = local.fullname
#   bandwidth       = 1000
# }

# resource "alicloud_eip_association" "main" {
#   allocation_id = alicloud_eip_address.main.id
#   instance_id   = alicloud_nat_gateway.main.id
# }

# ## add node vswitch to snat,accept node access Internet
# resource "alicloud_snat_entry" "vswitch-prod-map-cn-shenzhen-e" {
#   snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
#   source_vswitch_id = "vsw-wz9bujt34c241k8nqk2x4"
#   snat_ip           = alicloud_eip_address.main.ip_address
#   depends_on        = [alicloud_eip_association.main]
# }

# resource "alicloud_snat_entry" "vswitch-prod-map-cn-shenzhen-f" {
#   snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
#   source_vswitch_id = "vsw-wz91tirqacdyupzp61xvk"
#   snat_ip           = alicloud_eip_address.main.ip_address
#   depends_on        = [alicloud_eip_association.main]
# }

# ## add pod vswitch to snat,accept pod access Internet
# resource "alicloud_snat_entry" "vswitch-prod-ack-map-cluster-cn-shenzhen-e" {
#   snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
#   source_vswitch_id = "vsw-wz91bemf961h42qjfmnlk"
#   snat_ip           = alicloud_eip_address.main.ip_address
#   depends_on        = [alicloud_eip_association.main]
# }
# resource "alicloud_snat_entry" "vswitch-prod-ack-map-cluster-cn-shenzhen-f" {
#   snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
#   source_vswitch_id = "vsw-wz9wdtltxu5ezkqnijskp"
#   snat_ip           = alicloud_eip_address.main.ip_address
#   depends_on        = [alicloud_eip_association.main]
# }

resource "alicloud_cs_edge_kubernetes" "main" {
  name                          = "stg-ack-edge-cluster"
  count                         = 1
  version                       = "1.18.8-aliyunedge.1"
  worker_vswitch_ids            = ["vsw-wz9dmjf3cwwiznco6fuwf", "vsw-wz9l2fpxwbmbnpfclplsv"]
  pod_cidr                      = "10.244.48.0/21"
  service_cidr                  = "10.244.56.0/21"
  node_cidr_mask                = "26"  #per node 62 pods, cluster availabel scale to 33 nodes
  install_cloud_monitor         = true
  proxy_mode                    = "ipvs"
  new_nat_gateway               = true
  is_enterprise_security_group  = true
  slb_internet_enabled          = true
  worker_instance_types         = ["ecs.c5.xlarge"]
  worker_number                 = 2
  worker_disk_category          = "cloud_essd"
  worker_disk_size              = "80"
  # password                      = "Just4Test"
  key_name                      = "devops-key"
 
  dynamic "addons" {
      for_each = var.cluster_addons
      content {
        name                    = lookup(addons.value, "name", var.cluster_addons)
        config                  = lookup(addons.value, "config", var.cluster_addons)
        disabled                = lookup(addons.value, "disabled", var.cluster_addons)
      }
  }
  tags                          = local.tags
}